<?php
namespace Home\Controller;
use Think\Controller;
// 开发者：杨晓杰，
// 邮箱：2902468225@qq.com 


// 资讯控制器，负责资讯相关操作
class NewsController extends Controller {
	private $dbName='news';
	// 资讯搜索列表
	public function listPage(){
		tag('func');
		$key=I('get.key');
        // 可从标题，内容，描述搜索
        $where['title|contain|desc']=array('like',"%".$key."%");
		$all=M($this->dbName)->where($where)->order('id desc')->select();
		$data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));
		//添加企业名
		$length=count($data['list']);
		for($i=0;$i<$length;$i++){
			if($data['list'][$i]['cam_id']!=0){
				$campany=M('campany')->where('id = '.$data['list'][$i]['cam_id'])->find();
				$data['list'][$i]['company_name']=$campany['name'];
			}
		}
		$data['key']=$key;
		$this->assign('data',$data);
		// dump($data);
        R('Page/newsList');
	}
	// 最新资讯列表
	public function latest(){
		tag('func');
		$all=M($this->dbName)->order('id desc')->select();
		$data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));

		//添加企业名
		$length=count($data['list']);
		for($i=0;$i<$length;$i++){
			if($data['list'][$i]['cam_id']!=0){
				$campany=M('campany')->where('id = '.$data['list'][$i]['cam_id'])->find();
				$data['list'][$i]['company_name']=$campany['name'];
			}
		}

		$this->assign('data',$data);
		// dump($data);
        R('Page/latestNews');
	}
	// 企业资讯列表
	public function cpn(){
		tag('func');
		$all=M($this->dbName)->where('cam_id != 0')->order('id desc')->select();
		$data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));

		//添加企业名
		$length=count($data['list']);
		for($i=0;$i<$length;$i++){
			$campany=M('campany')->where('id = '.$data['list'][$i]['cam_id'])->find();
			$data['list'][$i]['company_name']=$campany['name'];
		}
		
		$this->assign('data',$data);
		// dump($data);
        R('Page/cpnNews');
	}
	// 平台资讯列表
	public function plat(){
		tag('func');
		$all=M($this->dbName)->where('cam_id = 0')->order('id desc')->select();
		$data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));

		$this->assign('data',$data);
		// dump($data);
        R('Page/platNews');
	}
	// 某个公司的资讯列表
	public function cpnList(){
		tag('func');
		$where['cam_id']=I('get.cam_id');
		$all=M($this->dbName)->where($where)->order('id desc')->select();
		$data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));
		$this->assign('data',$data);
		// dump($data);
        R('Page/cpnNewsList');
	}
	// 资讯详情
	public function detail(){
		tag('func');
		$id=I('get.id')?I('get.id'):1;

		$cam_id=M($this->dbName)->where('id = '.$id)->find();
		$cam_id=$cam_id['cam_id'];
		if($cam_id==0)$data['detail']=M($this->dbName)->where('id = '.$id)->find();
		else{
			$data['detail']=M($this->dbName)->join('__CAMPANY__ ON __NEWS__.cam_id = __CAMPANY__.id')->where('tswc_news.id ='.$id)->find();
		}

		//html转义
		$data['detail']['contain']=htmlspecialchars_decode($data['detail']['contain']);

		$this->assign('data',$data);
		// dump($data);
        R('Page/newsDetail');
	}
}